package com.zy.test.handler;

import android.content.Intent;
import android.os.Build;

import androidx.annotation.NonNull;

import com.zy.activity.BaseMenuActivity;
import com.zy.activity.bean.BaseMenuBean;
import com.zy.utils.ZYThreadUtils;

import java.util.ArrayList;

public class HandlerTestMenuActivity extends BaseMenuActivity {

    @Override
    protected void init(@NonNull ArrayList<BaseMenuBean> list) {
        list.add(new BaseMenuBean("Handler测试"));
        list.add(new BaseMenuBean("动画图解", 0));
        list.add(new BaseMenuBean("发送普通消息", 1));
        list.add(new BaseMenuBean("发送异步消息", 2));
        list.add(new BaseMenuBean("发送同步屏障", 3));
        list.add(new BaseMenuBean("移除同步屏障", 4));
        list.add(new BaseMenuBean("测试1(普通)", 10));
        list.add(new BaseMenuBean("测试2(异步)", 11));
        list.add(new BaseMenuBean("测试3(普通+异步)", 12));
        list.add(new BaseMenuBean("测试4(普通+屏障)", 13));
        list.add(new BaseMenuBean("测试5(异步+屏障)", 14));
        list.add(new BaseMenuBean("测试6(普通+异步+屏障)", 15));


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ZYThreadUtils.addIdleHandler();
            ZYThreadUtils.addMainIdleHandler();
        }
    }

    private static int token;

    @Override
    protected void clickItem(BaseMenuBean bean) {
        if (bean == null) {
            return;
        }
        if (bean.getCode() == 0) {
            Intent intent = new Intent(this, HandlerTestMainActivity.class);
            startActivity(intent);
        } else if (bean.getCode() == 1) {
            ZYThreadUtils.setAsynchronous();
        } else if (bean.getCode() == 2) {
            ZYThreadUtils.setAsynchronous();
        } else if (bean.getCode() == 3) {
            token = ZYThreadUtils.postSyncBarrier();
        } else if (bean.getCode() == 4) {
            ZYThreadUtils.removeSyncBarrier(token);
        } else if (bean.getCode() == 10) {
            ZYThreadUtils.setCommonMsgSleep();
            ZYThreadUtils.setCommonMsg();
            ZYThreadUtils.setCommonMsg();
            ZYThreadUtils.setCommonMsg();
            ZYThreadUtils.setCommonMsg();
            ZYThreadUtils.setCommonMsg();
            ZYThreadUtils.setCommonMsg();
        } else if (bean.getCode() == 11) {
            ZYThreadUtils.setCommonMsgSleep();
            ZYThreadUtils.setAsynchronous();
            ZYThreadUtils.setAsynchronous();
            ZYThreadUtils.setAsynchronous();
            ZYThreadUtils.setAsynchronous();
            ZYThreadUtils.setAsynchronous();
            ZYThreadUtils.setAsynchronous();
        } else if (bean.getCode() == 12) {
            ZYThreadUtils.setCommonMsgSleep();
            ZYThreadUtils.setCommonMsg();
            ZYThreadUtils.setAsynchronous();
            ZYThreadUtils.setCommonMsg();
            ZYThreadUtils.setAsynchronous();
            ZYThreadUtils.setCommonMsg();
            ZYThreadUtils.setAsynchronous();
        } else if (bean.getCode() == 13) {
            ZYThreadUtils.setCommonMsgSleep();
            ZYThreadUtils.setCommonMsg();
            ZYThreadUtils.setCommonMsg();
            ZYThreadUtils.setCommonMsg();
            token = ZYThreadUtils.postSyncBarrier();
            ZYThreadUtils.setCommonMsg();
            ZYThreadUtils.setCommonMsg();
            ZYThreadUtils.setCommonMsg();
        } else if (bean.getCode() == 14) {
            ZYThreadUtils.setCommonMsgSleep();
            ZYThreadUtils.setAsynchronous();
            ZYThreadUtils.setAsynchronous();
            ZYThreadUtils.setAsynchronous();
            token = ZYThreadUtils.postSyncBarrier();
            ZYThreadUtils.setAsynchronous();
            ZYThreadUtils.setAsynchronous();
            ZYThreadUtils.setAsynchronous();
        } else if (bean.getCode() == 15) {
            ZYThreadUtils.setCommonMsgSleep();
            ZYThreadUtils.setAsynchronous();
            ZYThreadUtils.setCommonMsg();
            token = ZYThreadUtils.postSyncBarrier();
            ZYThreadUtils.setCommonMsg();
            ZYThreadUtils.setAsynchronous();
            ZYThreadUtils.setCommonMsg();
            ZYThreadUtils.setAsynchronous();
        }
    }
}